package com.zhouyu.exampledesignmoudle.java8;

import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/**
 * Description:
 *
 * @author: zhou yu
 * @create: 2023-01-03 18:52
 */
public class HuaweiTestCode {

    public HuaweiTestCode() {
    }

    /**
     * 字符倒序
     *
     * @param
     */
    public void test1() {
        Scanner scanner = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (scanner.hasNext()) { // 注意 while 处理多个 case
            String line = scanner.nextLine();
            String reverseStr = new StringBuffer().append(line).reverse().toString();
            System.out.println(reverseStr);
        }
    }

    /**
     * 字符逆序
     */
    @Test
    public void test14() {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        StringBuilder res = new StringBuilder(input);
        System.out.println(res.reverse());
    }

    /**
     * 句子逆序
     */
    @Test
    public void test4() {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] array = line.split(" ");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
        }
    }

    /**
     * 字符单词分割，求最后的单词长度
     *
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] s = str.split(" "); //正则表达式实用性更强( str.split("\\s+"))
        int length = s[s.length - 1].length();
        System.out.println(length);

    }

    /**
     * 单词倒排
     */
    @Test
    public void test13() {
        Scanner scanner = new Scanner(System.in);
        String[] splitStr = scanner.nextLine().split("[^a-zA-Z]"); //非字母都用作分隔符
        StringBuilder str = new StringBuilder();
        for (int i = splitStr.length - 1; i >= 0; i--) {
            str.append(splitStr[i]);
            if (i != 0) {
                str.append(" ");
            }
        }
        System.out.println(str.toString());
    }

    /**
     * 字符串长度分割，后边补0
     */
    @Test
    public void test6() {
        Scanner sc = new Scanner(System.in);
        String str;
        while (sc.hasNext()) {
            str = sc.nextLine();
            while (str.length() > 8) {
                System.out.println(str.substring(0, 8));
                str = str.substring(8);
            }
            str = str + "00000000";
            System.out.println(str.substring(0, 8));
        }
    }

    /**
     * 统计字符串指定字符个数
     */
    public void test8() {
        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        String input2 = sc.nextLine();
        String split3 = input1.toUpperCase().replaceAll(input2.toUpperCase(), "");
        System.out.println(input1.length() - split3.length());
    }

    /**
     * 统计字符串中含有的不同字符的个数
     */
    public void test9() {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.next();
        //总共有128个字符。字需要用128位  import java.util.BitSet;
        BitSet bitSet = new BitSet(128);
        for (char c : line.toCharArray()) {
            //判断字符c是否已出现
            if (!bitSet.get(c)) {
                //未出现就设置为已出现
                bitSet.set(c);
            }
        }
        //统计有多少字符已出现过
        System.out.println(bitSet.cardinality());
    }

    /**
     * 统计字符其中英文字符，空格字符，数字字符，其他字符的个数
     */
    public void test15() {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.nextLine();
            String s1 = str.replaceAll("[A-Z]+|[a-z]+", "");
            System.out.println(str.length() - s1.length());
            String s2 = s1.replaceAll(" ", "");
            System.out.println(s1.length() - s2.length());
            String s3 = s2.replaceAll("[0-9]+", "");
            System.out.println(s2.length() - s3.length() + "\n" + s3.length());
        }
    }

    /**
     * 统计大写字符个数
     */
    public void test16() {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(in.nextLine().replaceAll("[^A-Z]", "").length());
        }
    }


    /**
     * 字符串按照字典序排序
     */
    @Test
    public void test5() {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String[] ss = new String[num];
        for (int i = 0; i < num; i++) {
            ss[i] = sc.next();
        }
        //import java.util.Arrays;
        Arrays.sort(ss);
        for (int i = 0; i < ss.length; i++) {
            System.out.println(ss[i]);
        }
    }

    /**
     * 字符串按照ASC排序
     */
    @Test
    public void test17() {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a[] = new int[128];
            String str = in.next();
            for (int i = 0; i < str.length(); i++) {
                int k = str.charAt(i);//统计出现次数
                a[k]++;
            }
            for (int j = 48; j < a.length; j++) {//从'0'开始输出
                if (a[j] != 0)
                    for (int b = 0; b < a[j]; b++)
                        System.out.print((char) j);
            }
            System.out.println();
        }
    }

    /**
     * 按照字符次数顺序统计，次数相同按照ASC排序
     */
    public void test12() {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();

        //将字符存入TreeMap import java.util.TreeMap;
        TreeMap<Character, Integer> map = new TreeMap<>();
        for (int i = 0; i < str.length(); i++) {
            if (!map.containsKey(str.charAt(i))) {
                map.put(str.charAt(i), 1);
            } else {
                map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1);
            }
        }

        //找到max值
        int max = 0;
        for (int val : map.values()) {
            if (val > max)
                max = val;
        }

        //输出
        while (max > 0) {
            for (char key : map.keySet()) {
                if (map.get(key) == max)
                    System.out.print(key);
            }
            max--;
        }
    }

    /**
     * 删除出现次数最少的字符
     */
    public void test11() {
        //import java.util.Collections;
        //import java.util.HashMap;
        //import java.util.Collection;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            char[] chars = s.toCharArray();
            //统计每个字母的数量
            HashMap<Character, Integer> map = new HashMap<>();
            for (char aChar : chars) {
                map.put(aChar, (map.getOrDefault(aChar, 0) + 1));
            }
            //找到数量最少的字符数量
            Collection<Integer> values = map.values();
            Integer min = Collections.min(values);

            //用空字符串替换该字母
            for (Character character : map.keySet()) {
                if (map.get(character) == min) {
                    s = s.replaceAll(String.valueOf(character), "");
                }
            }
            System.out.println(s);
        }
    }


    /**
     * 16进制转10进制
     */
    @Test
    public void test7() {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            System.out.println(Integer.parseInt(s.substring(2, s.length()), 16));
        }
    }


    /**
     * 取近似值
     */
    @Test
    public void test3() {
        Scanner in = new Scanner(System.in);
        float num = in.nextFloat();
        System.out.println(Math.round(num));
    }


    /**
     * 简单密码
     */
    public void test10() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) >= 97 && s.charAt(i) <= 114) {
                    System.out.print((s.charAt(i) - 97) / 3 + 2);
                } else if (s.charAt(i) == 115) {
                    System.out.print(7);
                } else if (s.charAt(i) >= 116 && s.charAt(i) <= 118) {
                    System.out.print(8);
                } else if (s.charAt(i) >= 119 && s.charAt(i) <= 122) {
                    System.out.print(9);
                } else if (s.charAt(i) >= 65 && s.charAt(i) <= 89) {
                    System.out.print((char) (s.charAt(i) + 33));
                } else if (s.charAt(i) == 90) {
                    System.out.print('a');
                } else {
                    System.out.print(s.charAt(i));
                }
            }
        }
    }

    /**
     * 密码等级强度
     */
    @Test
    public void test20(){
        Scanner in = new Scanner(System.in);

        while(in.hasNextLine()){
            char[] ch = in.nextLine().toCharArray();
            int score = 0;

            //统计长度
            int len = ch.length;
            if(len <= 4) score += 5;
            else if(len >4 && len < 8) score += 10;
            else score += 25;

            //遍历获取大小写字母、数字、符号数目
            int upCount = 0;
            int lowCount = 0;
            int numCount = 0;
            int sigCount = 0;

            for (int i = 0; i < len; i++) {
                if(Character.isUpperCase(ch[i])) ++upCount;
                else if(Character.isLowerCase(ch[i])) ++lowCount;
                else if(Character.isDigit(ch[i])) ++numCount;
                else ++sigCount;
            }

            //字母分数
            if((upCount > 0 && lowCount == 0) || (upCount == 0 && lowCount > 0)) score += 10;
            else if(upCount > 0 && lowCount > 0) score += 20;
            else score += 0;

            //数字分数
            if(numCount == 1) score += 10;
            else if(numCount > 1) score += 20;
            else score += 0;

            //符号分数
            if(sigCount == 1) score += 10;
            else if(sigCount > 1) score += 25;
            else score += 0;

            //奖励分数
            if (numCount > 0 && upCount > 0 && lowCount > 0 && sigCount > 0) score += 5;
            else if(numCount > 0 && sigCount > 0 &&(upCount >0 || lowCount >0)) score += 3;
            else if(numCount > 0 &&(upCount >0 || lowCount >0)) score += 2;

            //评分
            if(score >= 90) System.out.println("VERY_SECURE");
            else if(score >= 80) System.out.println("SECURE");
            else if(score >= 70) System.out.println("VERY_STRONG");
            else if(score >= 60) System.out.println("STRONG");
            else if(score >= 50) System.out.println("AVERAGE");
            else if(score >= 25) System.out.println("WEAK");
            else System.out.println("VERY_WEAK");

        }
    }

    /**
     * 字符串包含（长和短）
     */
    @Test
    public void test18() {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String shortstr = in.nextLine();
            String longstr = in.nextLine();
            //import java.util.HashSet; import java.util.Set;
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < shortstr.length(); ++i) {
                set.add(shortstr.charAt(i));
            }
            for (int i = 0; i < longstr.length(); ++i) {
                set.remove(longstr.charAt(i));
            }
            System.out.println(set.size() == 0);
        }
    }

    /**
     * 字符前后数字加上*（表示数字）
     */
    public void test19(){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String input = scanner.next();
            System.out.println(input.replaceAll("([0-9]+)", "*$1*")); //把所有的数字段提取出来，前后加上星号再放回去
        }
    }

    /**
     * 最长回文字符串
     */
    public void test21(){
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int max = 0;
        /**
         *双指针遍历找到最长子串
         */
        for (int i = 0; i < s.length(); i++) {
            for (int j = s.length(); j > i; j--) {
                String toBeJuged = s.substring(i, j);
                if (isPalindromeString(toBeJuged)) {
                    max = Math.max(max, j - i);
                }
            }
        }
        System.out.print(max);
    }
    /**
     *判断一个字符串是否是回文字符串的方法
     */
    static boolean isPalindromeString(String s) {
        return s.equals(new StringBuilder(s).reverse().toString());
    }

    @Test
    public void test22(){
        Scanner in=new Scanner(System.in);
        int y=in.nextInt();
        int m=in.nextInt();
        int d=in.nextInt();
        Calendar c1= Calendar.getInstance();//实例化
        c1.set(y, m-1, d);//注意月份从0开始
        System.out.println(c1.get(Calendar.DAY_OF_YEAR));
    }


}
